package reflection.class_;

import car.Car;

/**
 * @author DCSGO
 * @version 1.0
 * 获取 Class 对象的常用方式
 */
public class GetClass_ {
    public static void main(String[] args) throws ClassNotFoundException {
        //1. Class.forName(全类名) 应用场景：通过读取配置文件获得
        String classAllPath = "car.Car";
        Class<?> cls = Class.forName(classAllPath);
        System.out.println(cls);//class car.Car

        //2. 类名.class 应用场景：方法传参(构造对象中的参数...)
        System.out.println(Car.class);//class car.Car

        //3. 对象实例.getClass()
        Car car = new Car();
        System.out.println(car.getClass());//class car.Car

        //4. 通过加载器获得 Class 对象
        //(1). 获得一个类加载器
        ClassLoader classLoader = car.getClass().getClassLoader();
        //(2). 通过加载器获得 Class 对象
        Class<?> cls2 = classLoader.loadClass(classAllPath);
        System.out.println(cls2);//class car.Car
        ClassLoader classLoader1 = Car.class.getClassLoader();
        Class<?> cls3 = classLoader1.loadClass(classAllPath);
        System.out.println(cls3);//class car.Car

        //当然上面的 Class 对象 都是同一个对象
        System.out.println(cls.hashCode());//460141958
        System.out.println(cls2.hashCode());//460141958
        System.out.println(cls3.hashCode());//460141958

        //5. 基本数据类型获得其对应的 Class 对象
        // 数据类型.class
        Class<Integer> integerClass = int.class;
        Class<Character> characterClass = char.class;
        Class<Boolean> booleanClass = boolean.class;
        System.out.println(integerClass);//int

        //6. 包装类获得其对应的 Class 对象
        // 包装类名.TYPE
        Class<Integer> integerClass1 = Integer.class;
        System.out.println(integerClass1);//class java.lang.Integer
        Class<Integer> type = Integer.TYPE;
        System.out.println(type);//int

        System.out.println(type.hashCode());//1163157884
        System.out.println(integerClass.hashCode());//1163157884
    }
}
